home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Library
/
RoseWare - Network Support Library.iso
/
manage
/
gtree.arc
/
HUNT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-03-17
|
13KB
|
375 lines
/**********************************************************************/
/* */
/* HUNT.C */
/* */
/* This will search an entire drive for a given file spec. Certain */
/* options are available. See the notes below for detail on these */
/* options and command line switches. */
/* */
/**********************************************************************/
/* */
/* Written October 21, 1985 */
/* */
/* Author David Midkiff -- D. M. Software And Systems */
/* */
/**********************************************************************/
/* */
/* Modified December 13, 1986 */
/* David Midkiff -- D. M. Software And Systems */
/* Added the ability to delete the specified files. */
/* */
/**********************************************************************/
/* */
/* Modified January 29, 1987 */
/* David Midkiff -- D. M. Software And Systems */
/* Changed the delete function to ask for operator */
/* verification beforing deleting files, and the -v */
/* command line switch to "pre"-verify deletions. */
/* */
/**********************************************************************/
/* */
/* Modified December 1, 1987 */
/* David Midkiff -- D. M. Software And Systems */
/* This code was ported from the Lattice C compiler to */
/* Turbo C. */
/* */
/**********************************************************************/
/* */
/* Modified December 23, 1987 */
/* David Midkiff -- D. M. Software And Systems */
/* This was becoming unmanagable, so I removed the code */
/* for file options and just it print a disk tree and I */
/* changed the name to GTREE.C */
/* */
/**********************************************************************/
/* */
/* Copyright (c) 1988 -- D. M. Software And Systems */
/* */
/* All rights reserved. Do not redistribute this program without */
/* this notice, or the notice in the main() function. */
/* */
/* This program is designed to be instructive and useful, however */
/* there are no warranties expressed or implied. Feel free to */
/* use this program in any manner you see fit, with the exceptions */
/* of commerical environments and redistribution for any fee. */
/* You are feel free to redistribute this program to any bulletin */
/* boards or user's group, provide no fee is charge for any service */
/* you may provide. */
/* */
/* If you find this program useful, you are asked to send a $20 */
/* contribution towards its upkeep. No support will be given to */
/* unsupported users, except in the cases where the user needs to */
/* find out if this program is suitable for his or her purposes. */
/* */
/* Send $20.00 contribution to: */
/* D. M. Software And Systems */
/* 6572 Suson Woods Drive */
/* St. Louis, MO 63128 */
/* */
/* Remember, you do not have to send a contribution, but your */
/* conscience will haunt you forever. */
/* */
/**********************************************************************/
#include "stdio.h"
#include "conio.h"
#include "ctype.h"
#include "dir.h"
#include "dos.h"
#include "io.h"
#include "\dmlist\mylib.h"
#include "stdlib.h"
#include "string.h"
extern unsigned _stklen = 32678U ; /* Need a larger than normal stack */
/* since buildtree() is recursive. */
/*PAGE*/
/*******************************************************/
/* */
/* --- GLOBAL VARIABLE DEFINITIONS --- */
/* */
/*******************************************************/
#define ESCAPE (char) 27
#define MAXFILES 20
long bytesdeleted = 0L ; /* Total bytes deleted */
int drive = -1 ; /* Drive to use as a default */
int filesdeleted = 0 ; /* Number of files deleted */
int killflag = 0 ; /* 1 == Delete matching files */
int matches = 0 ; /* Number of files matching entered file specs */
char scratch[256] ; /* A scratch string */
char spaces[81] ; /* A string of spaces for formatting output */
int spec_count = 0 ; /* A count of inputted file spec's */
char specin[MAXFILES][80] ; /* The inputted file spec's */
long totalbytes = 0L ; /* Total bytes in all matching files */
int verified = 0 ; /* 1 == Deletion request verified on the command line */
/*PAGE*/
/***********************************/
/* */
/* FUNCTION PROTOTYPES */
/* */
/***********************************/
void buildtree( char *thisdirectory ) ;
void fatalerr( char *message , int useflag ) ;
void showusage( void ) ;
/*PAGE*/
/**********************************************************************/
/* */
/* BUILDTREE() - Build a directory tree from the passed name. It */
/* does not really build a tree since it is recursive */
/* function. Care should be used when changing this */
/* routine!!!! It works as it stands now. */
/* */
/**********************************************************************/
void buildtree( p )
char *p ; /* Directory being searched */
{
struct ffblk dir ; /* For directories */
int deleteflag ; /* For deletion request */
struct ffblk file ; /* For data files */
int i ; /* Scratch integer */
char newname[81] ; /* String for formatting a name */
printf( "\n%s" , p ) ;
for( i=0; i<spec_count; i++ )
{
(void) sprintf( newname , "%s%s" , p , &specin[i][0] ) ;
if( !findfirst( newname , &file , 0 ) )
{
do
{
printf( "\n%*s%-12.12s" , strlen(p) , "" , file.ff_name ) ;
totalbytes = totalbytes + file.ff_fsize ;
matches++ ;
if( killflag )
{
if( !verified )
{
printf( " -- Delete (Y or N)? " ) ;
do
{
deleteflag = toupper( getche() ) ;
if( deleteflag == ESCAPE )
exit( 2 ) ;
} while( deleteflag != 'Y' && deleteflag != 'N' ) ;
}
if( deleteflag == 'Y' || verified )
{
(void) sprintf( scratch , "%s%s" , p , file.ff_name ) ;
if( unlink( scratch ) )
printf( " -- cannot be delete" ) ;
else
{
printf( " -- Deleted" ) ;
bytesdeleted = bytesdeleted + file.ff_fsize ;
filesdeleted++ ;
} /* if( unlink( scratch ) )....else....*/
} /* if( deleteflag == 'Y' || verified ) */
else
printf( " -- ignored" ) ;
} /* if( killflag ) */
} while( !findnext( &file ) ) ;
} /* if( !findfirst( newname , &file , 0 ) ) */
} /* for( i=0; i<spec_count; i++ ) */
(void) sprintf( newname , "%s*.*" , p ) ;
if( !findfirst( newname , &dir , FA_DIREC ) )
{
do
{
if( dir.ff_attrib == FA_DIREC && dir.ff_name[0] != '.' )
{
(void) sprintf( newname , "%s%s\\" , p , dir.ff_name ) ;
buildtree( newname ) ;
} /* if( dir.ff_attrib == FA_DIREC && dir.ff_name[0] != '.' ) */
} while( !findnext( &dir ) ) ;
} /* if( !findfirst( newname , dir , FA_DIREC ) ) */
return ;
} /* end of buildtree() */
/*PAGE*/
/**********************************************************************/
/* */
/* FATALERR() - Print a fatal error message and exit after setting */
/* the DOS ERRORLEVEL variable to 1. */
/* */
/**********************************************************************/
void fatalerr( msg , useflag )
char *msg ; /* Part of error message to print */
int useflag ; /* 1 == Show usage before exiting */
{
printf( "\n %s -- HUNT canceled\n" , msg ) ;
if( useflag )
showusage() ;
exit( 1 ) ;
return ; /* sic */
} /* end of fatal error */
/*page*/
/**********************************************************************/
/* */
/* MAIN() - This is the main processing function. The only things */
/* that are processed here are command line parameters. */
/* The process is: */
/* 1.) Process command line arguements. */
/* 2.) Check for a drive assignment. If not present */
/* use the default drive. */
/* 3.) Format the root file name by inserting the */
/* drive letter to give C:\, for example. */
/* 4.) Call the buildtree() function with the root */
/* direcorty to get started. */
/* 5.) Print any of the requested statistics. */
/* */
/**********************************************************************/
void main( argc , argv )
int argc ;
char *argv[] ;
{
int i ; /* A scratch integer */
char root[8] ; /* For the formatted root file name */
(void) puts( "HUNT -- Version 1.0 - March 1988\nCopyright (c) 1988. D. M. Software and Systems. All rights reserved." ) ;
if( argc == 1 )
fatalerr( "I have no work to do!!" , 1 ) ;
(void) sprintf( spaces , "%80.80s" , "" ) ; /* Make an 80 character string of spaces */
for( i=1; i<argc; i++ ) /* Parse the command line arguments */
{
if( *argv[i] != '-' && *argv[i] != '/' )
{
if( *(argv[i]+1) != ':' )
strupper( &specin[spec_count++][0] , argv[i] ) ;
else
{
if( drive != -1 )
fatalerr( "More than one drive specification passed" , 1 ) ;
drive = tolower( *argv[i] ) ;
if( strlen( argv[i] ) != 2 || drive < 'a' || drive > 'z' )
fatalerr( "Invalid drive specification" , 1 ) ;
} /* if( *(argv[i]+1) != ':' ) */
} /* if( *argv[i] != '-' && *argv[i] != '/' ) */
else
{
switch( tolower( *(argv[i]+1) ) )
{
case 'h' : /* Show usage only */
showusage() ;
exit( 1 ) ;
break ;
case 'k' : /* Set the kill flag */
killflag = 1 ;
break ;
case 'v' : /* Pre-verify deletion request */
verified = 1 ;
break ;
default : /* Invalid switch */
(void) sprintf( scratch , "%s is an invalid parameter" , argv[i] ) ;
fatalerr( scratch , 0 ) ;
} /* end case/switch( tolower( *(argv[i]+1) ) ) */
} /* if( *argv[i] != '-' && *argv[i] != '/' ) */
} /* for( i=1; iargc; i++ ) */
if( drive == -1 ) /* Set drive to the current drive */
drive = getdisk() ; /* of not set by the operator. */
drive += 'A' ; /* Convert to alpha */
(void) sprintf( root , "%c:\\" , drive ) ; /* Format for buildtree()'s initial call */
buildtree( root ) ;
printf( "\n%ld bytes found in %d files." , totalbytes , matches ) ;
if( killflag )
printf( " %ld bytes and %d files were deleted." , bytesdeleted , filesdeleted ) ;
exit( 0 ) ;
} /* end of main() */
/**********************************************************************/
/* */
/* SHOWUSAGE() - Prints the usage message that appears at runtime. */
/* This can be called by a -h on the command line or */
/* by certan errors via fatalerr() above. */
/* */
/**********************************************************************/
void showusage()
{
(void) puts( "\nUsage is hunt [drive:] [-flags] [filespec] where:" ) ;
(void) puts( "\tdrive: - to override default drive specification." ) ;
(void) puts( "\t-H for help. Displays this message." ) ;
(void) puts( "\t-K for kill. Delete matching files." ) ;
(void) puts( "\t-V for verify. \"Preverify\" deletion request." ) ;
return ;
} /* end of showusage() */
/*******************************************************/
/* */
/* STRUPPER() */
/* */
/* Copies a string and converts it to upper case. */
/* On Calling: */
/* dest == the destionation string. */
/* src == The source string. */
/* Returns: */
/* Nothing */
/* */
/*******************************************************/
void strupper( dest , src )
char *dest ; /* Destination string */
char *src ; /* Source string */
{
while( *src )
{
*dest++ = toupper( *src ) ; /* BE CAREFUL...toupper is a macro */
src++ ;
} /* while( *dest ) */
*dest = '\0' ;
return ;
} /* end of strupper() */
/*************************/
/* */
/* END OF HUNT.C */
/* */
/*************************/